package com.dbappsecurity.network.client;

import android.util.Log;
import com.dbappsecurity.network.client.interceptor.CacheInterceptor;
import com.dbappsecurity.network.client.interceptor.HeaderInterceptor;
import com.dbappsecurity.network.client.interceptor.TraversalParameterInterceptor;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by Arirus on 2017/8/11.
 */

//OkHttpClient 通用Client 非单例
public class NetworkClient {

  private static String TAG = "NetworkClient";

  private OkHttpClient mClient;

  public NetworkClient(OkHttpClient client) {
    this.mClient = client;
  }

  public OkHttpClient getClient() {
    return mClient;
  }

  public void setClient(OkHttpClient client) {
    this.mClient = client;
  }

  //静态方法
  public static NetworkClient newInstance(Logger level) {

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(message -> {
      Log.i("NetworkClient", "Arirus newInstance: ");
    });

    loggingInterceptor.setLevel(Logger.trans(level));

    TraversalParameterInterceptor traversalParameterInterceptor =
        new TraversalParameterInterceptor();

    return new NetworkClient(new OkHttpClient.Builder().addInterceptor(new HeaderInterceptor())
        .addInterceptor(loggingInterceptor)
        .addInterceptor(traversalParameterInterceptor)
        .addNetworkInterceptor(new CacheInterceptor())
        .connectTimeout(15, TimeUnit.SECONDS)
        .readTimeout(15, TimeUnit.SECONDS)
        .writeTimeout(15, TimeUnit.SECONDS)
        .build());
  }

  //实例方法
  public <T extends Interceptor> NetworkClient replaceInterceptor(T srcInterceptor) {
    List<Interceptor> list = getClient().interceptors();
    Class srcClass = srcInterceptor.getClass();

    OkHttpClient.Builder builder = getClient().newBuilder();
    builder.interceptors().clear();

    for (Interceptor interceptor : list) {
      Class dstClass = interceptor.getClass();
      if (dstClass.equals(srcClass)) {
        builder.addInterceptor(srcInterceptor);
      } else {
        builder.addInterceptor(interceptor);
      }
    }

    setClient(builder.build());
    return this;
  }

  public NetworkClient addInterceptor(Interceptor interceptor) {
    return addInterceptors(new Interceptor[] { interceptor });
  }

  public NetworkClient addInterceptors(Interceptor[] interceptors) {
    OkHttpClient.Builder newBuilder = getClient().newBuilder();
    for (Interceptor i : interceptors) {
      newBuilder.addInterceptor(i);
    }
    setClient(newBuilder.build());
    return this;
  }

  public enum Logger {
    NONE,

    BASIC,

    HEADERS,

    BODY;

    public static HttpLoggingInterceptor.Level trans(Logger logger) {
      switch (logger) {
        case NONE:
          return HttpLoggingInterceptor.Level.NONE;
        case BASIC:
          return HttpLoggingInterceptor.Level.BASIC;
        case HEADERS:
          return HttpLoggingInterceptor.Level.HEADERS;
        case BODY:
          return HttpLoggingInterceptor.Level.BODY;
        default:
          return HttpLoggingInterceptor.Level.NONE;
      }
    }
  }
}
